using System;
using System.Web.Mvc;

namespace etcetera.Mvc.UI.Progressbar
{
    public static class ProgressbarExtensions
    {
        public static IProgressbar Progressbar(this HtmlHelper helper, float percentage)
        {
            return new Progressbar(percentage);
        }

        public static IProgressbar Progressbar(this HtmlHelper helper, string viewDataKey)
        {
            object dataSource = helper.ViewContext.ViewData.Eval(viewDataKey);

            if (dataSource == null)
            {
                throw new InvalidOperationException(string.Format(
                                                        "Item in ViewData with key '{0}' is not of type float.",
                                                        viewDataKey));
            }

            return helper.Progressbar(Convert.ToSingle(dataSource));
        }

        public static IProgressbar WithId(this IProgressbar progressbar, string id)
        {
            return progressbar.WithId(id);
        }
    }
}